/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.vsu.cs.piit.vokod.reports;

import java.text.DateFormat;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.query.JRHibernateQueryExecuterFactory;
import ru.vsu.cs.piit.hibernate.HibernateSessionFactory;

/**
 *
 * @author Галчонок
 */
public class ReportFactory {
    public static String PATH="./reports/";
    protected static ReportXMLParser p = new ReportXMLParser(PATH+"Reports.xml");
    protected static Map<String,String> map = p.getReports();

    ReportFactory() {
    }

    public static JasperPrint getReport(String reportName, Map params)
    {
        params.put("DateFormat", DateFormat.getDateInstance(DateFormat.SHORT));
        params.put(JRHibernateQueryExecuterFactory.PARAMETER_HIBERNATE_SESSION, HibernateSessionFactory.getSession());

        try {
            JasperReport jasperReport = JasperCompileManager.compileReport(PATH + map.get(reportName));
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params);
            return jasperPrint;
        } catch (JRException ex) {
            Logger.getLogger(ReportFactory.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public static Object[] getExistReport()
    {
        return map.keySet().toArray();
    }

    public static List<ReportParam> getReportParams(String reportName){
        return p.getParams(reportName);
    }

}
